package net.atomtree.corrsepond.factory;

import net.atomtree.entity.Application;
import net.atomtree.entity.Job;
import net.atomtree.entity.Stage;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by czk on 2016/1/4.
 */
public class SparkRestFactoryAdapter extends SparkRestFactory {
    public static void main(String[] args) throws Exception {
        Application application=new Application("101.204.243.107","4040","sparkPI");
        String url=URLFactory(application);
        System.out.println(url);
    }
    public static String URLFactory(Object obj) throws Exception {
        if(obj.getClass().isInstance(Application.class)){
            Application application=(Application)obj;
            return URLFactory(application);
        }
        if(obj.getClass().isInstance(Stage.class)){
            Stage stage=(Stage)obj;
            return URLFactory(stage);
        }
        if(obj.getClass().isInstance(Job.class)){
            Job job=(Job)obj;
            return URLFactory(job);
        }
        throw new Exception("obj is null or error");
    }
    public static String URLFactory(Application application) throws Exception {
        Map<String, String> URLParam =new HashMap<String, String>();
        if (null!=application.port){
            URLParam.put("port",application.port);
        }
        if(null!=application.host){
            URLParam.put("host",application.host);
        }
        if (null!=application.appName){
            URLParam.put("appName",application.appName);
        }
        String appUrl=new  SparkRestFactory().URLFactory(URLParam,APPLICATION);
        return appUrl;
    }

    public static String URLFactory(Stage stage) throws Exception {
        Map<String, String> URLParam =new HashMap<String, String>();
        if (null!=stage.appName){
            URLParam.put("appName",stage.appName);
        }
        if (null!=stage.stageId){
            URLParam.put("stageId",stage.stageId);
        }
        if(null!=stage.attemptId){
            URLParam.put("attemptId",stage.attemptId);
        }
        String stageUrl=new  SparkRestFactory().URLFactory(URLParam,STAGE);
        return stageUrl;
    }

    public static String URLFactory(Job job) throws Exception {
        Map<String, String> URLParam =new HashMap<String, String>();
        if (null!=job.jobId){
            URLParam.put("jobId",job.jobId);
        }
        if(null!=job.appName){
            URLParam.put("appName",job.appName);
        }
        String jobUrl=new  SparkRestFactory().URLFactory(URLParam,JOB);
        return jobUrl;
    }


}
